/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JServiceEditor.java
 *
 * Created on 2009-9-15, 10:33:31
 */
package cn.ekuma.epos.servings;

import cn.ekuma.data.ui.swing.AbstractParentDAOJEditor;
import cn.ekuma.data.ui.swing.JChildDAOEditorPanel;
import cn.ekuma.epos.bean.util.SerivceHelper;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.servings.model.ServiceDayStateTableModel;
import com.openbravo.bean.AttributeSet;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;

import com.openbravo.bean.Place;
import com.openbravo.bean.Product;
import com.openbravo.bean.Service;
import com.openbravo.bean.ServiceCalendar;
import com.openbravo.bean.Tax;
import com.openbravo.bean.TaxCategory;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.JPlaceFinder;
import com.openbravo.pos.panels.JProductFinder;
import com.openbravo.pos.panels.JObjectListFinder;
import com.openbravo.pos.sales.TaxesLogic;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

/**
 *
 * @author Administrator
 */
public class JServiceEditor extends AbstractParentDAOJEditor<Service,String>{
    private final ComboBoxValModel serviceTypeModel;
    private final ComboBoxValModel rantCycleStyleModel;
    private final ComboBoxValModel roundRuleModel;
    private  ComboBoxValModel serviceCalendarModel;
    private final I_DataLogicSales m_dlSales;
     
    static SimpleDateFormat sdf=new SimpleDateFormat("ssyyddmm");

    JChildDAOEditorPanel dayStatePanel;
    private TaxesLogic taxeslogic;
    private ComboBoxValModel taxcatmodel;
    private ComboBoxValModel attmodel;

    private Object pricesell;
    private boolean priceselllock = false;
    private boolean reportlock = false;

    JServiceEditor(I_DataLogicSales m_dlSales, DirtyManager dirty) {
        super(dirty);
        initComponents();
        this.m_dlSales = m_dlSales;
        jServiceId.getDocument().addDocumentListener(dirty);
        jServiceType.addActionListener(dirty);
        m_jPriceSell.getDocument().addDocumentListener(dirty);
        jRantCycleMultiple.getDocument().addDocumentListener(dirty);
        m_jCustomerPrice.getDocument().addDocumentListener(dirty);
        jrantCycleStyle.addActionListener(dirty);
        jServiceCalendar.addActionListener(dirty);

        jEditProduct.addActionListener(dirty);

        serviceTypeModel = new ComboBoxValModel();
        serviceTypeModel.add(new ComboBoxValEntry(Service.SERVICETYPE_product, AppLocal.getIntString("list.SERVICETYPE_product")));
        serviceTypeModel.add(new ComboBoxValEntry(Service.SERVICETYPE_place, AppLocal.getIntString("list.SERVICETYPE_place")));
        serviceTypeModel.add(new ComboBoxValEntry(Service.SERVICETYPE_user, AppLocal.getIntString("list.SERVICETYPE_user")));
        jServiceType.setModel(serviceTypeModel);

        rantCycleStyleModel = new ComboBoxValModel();
        rantCycleStyleModel.add(new ComboBoxValEntry(Service.RANTCYCLESTYLE_everyMinute, AppLocal.getIntString("list.RANTCYCLESTYLE_everyTime")));
        rantCycleStyleModel.add(new ComboBoxValEntry(Service.RANTCYCLESTYLE_everyMinute, AppLocal.getIntString("list.RANTCYCLESTYLE_everyMinute")));
        rantCycleStyleModel.add(new ComboBoxValEntry(Service.RANTCYCLESTYLE_everyHour, AppLocal.getIntString("list.RANTCYCLESTYLE_everyHour")));
        rantCycleStyleModel.add(new ComboBoxValEntry(Service.RANTCYCLESTYLE_everyDay, AppLocal.getIntString("list.RANTCYCLESTYLE_everyDay")));
        rantCycleStyleModel.add(new ComboBoxValEntry(Service.RANTCYCLESTYLE_everyWeek, AppLocal.getIntString("list.RANTCYCLESTYLE_everyWeek")));
        rantCycleStyleModel.add(new ComboBoxValEntry(Service.RANTCYCLESTYLE_moon, AppLocal.getIntString("list.RANTCYCLESTYLE_moon")));
        rantCycleStyleModel.add(new ComboBoxValEntry(Service.RANTCYCLESTYLE_year, AppLocal.getIntString("list.RANTCYCLESTYLE_year")));
        jrantCycleStyle.setModel(rantCycleStyleModel);

        dayStatePanel=new JChildDAOEditorPanel(m_dlSales,new ServiceDayStateTableModel(),new JServiceDayStateEditor(new DirtyManager()));
        jTabbedPane1.add(dayStatePanel.getTabName(), dayStatePanel);
        addObjectChangeListener(dayStatePanel);

        roundRuleModel=new ComboBoxValModel();
        roundRuleModel.add(new ComboBoxValEntry(Service.ROUNDRULE_noRound, AppLocal.getIntString("list.ROUNDRULE_noRound")));
        roundRuleModel.add(new ComboBoxValEntry(Service.ROUNDRULE_roundDown, AppLocal.getIntString("list.ROUNDRULE_roundDown")));
        roundRuleModel.add(new ComboBoxValEntry(Service.ROUNDRULE_roundUp, AppLocal.getIntString("list.ROUNDRULE_roundUp")));
        roundRuleModel.add(new ComboBoxValEntry(Service.ROUNDRULE_round, AppLocal.getIntString("list.ROUNDRULE_round")));
        jServiceCalendar.setModel(roundRuleModel);

        FieldsManager fm = new FieldsManager();
        m_jPriceBuy.getDocument().addDocumentListener(fm);
        m_jPriceSell.getDocument().addDocumentListener(new PriceSellManager());
        m_jTax.addActionListener(fm);

        m_jPriceSellTax.getDocument().addDocumentListener(new PriceTaxManager());
        m_jmargin.getDocument().addDocumentListener(new MarginManager());

        m_jCustomerPrice.getDocument().addDocumentListener(new CustomerPriceSellManager());
        m_jCustomerPriceMargin.getDocument().addDocumentListener(new CustomerMarginManager());
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jServiceId = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jServiceName = new javax.swing.JTextField();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jEditProduct = new javax.swing.JButton();
        jrantCycleStyle = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jServiceType = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jServiceCalendar = new javax.swing.JComboBox();
        jRantCycleMultiple = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        m_jTax = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        m_jAtt = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        m_jPriceBuy = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        m_jPriceSell = new javax.swing.JTextField();
        m_jmargin = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        m_jPriceSellTax = new javax.swing.JTextField();
        jCustomerPriceLabel = new javax.swing.JLabel();
        m_jCustomerPrice = new javax.swing.JTextField();
        m_jCustomerPriceMargin = new javax.swing.JTextField();
        jMutex = new javax.swing.JCheckBox();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));

        jLabel1.setText(AppLocal.getIntString("label.sevice_ID")); // NOI18N

        jServiceId.setMaximumSize(new java.awt.Dimension(6, 21));

        jLabel18.setText(AppLocal.getIntString("label.sevice_Name")); // NOI18N

        javax.swing.GroupLayout gl_jPanel1 = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(gl_jPanel1);
        gl_jPanel1.setHorizontalGroup(
            gl_jPanel1.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gl_jPanel1.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jServiceId, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jServiceName, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(38, Short.MAX_VALUE))
        );
        gl_jPanel1.setVerticalGroup(
            gl_jPanel1.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gl_jPanel1.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_jPanel1.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(jServiceId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(gl_jPanel1.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel18)
                        .addComponent(jServiceName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        gl_jPanel1.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jServiceId});

        add(jPanel1);

        jEditProduct.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
        jEditProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jEditProductActionPerformed(evt);
            }
        });

        jLabel2.setText(AppLocal.getIntString("label.sevice_Type")); // NOI18N

        jLabel3.setText(AppLocal.getIntString("label.serviceCalendar")); // NOI18N

        jLabel15.setText(AppLocal.getIntString("label.rantCycleStyle")); // NOI18N

        jRantCycleMultiple.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel7.setText(AppLocal.getIntString("label.taxcategory")); // NOI18N

        jLabel13.setText(AppLocal.getIntString("label.attributes")); // NOI18N

        jLabel4.setText(AppLocal.getIntString("label.prodpricebuy")); // NOI18N

        m_jPriceBuy.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel5.setText(AppLocal.getIntString("label.prodpricesell")); // NOI18N

        m_jPriceSell.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        m_jmargin.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel16.setText(AppLocal.getIntString("label.prodpriceselltax")); // NOI18N

        m_jPriceSellTax.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jCustomerPriceLabel.setText(AppLocal.getIntString("label.baseCustomerPrice")); // NOI18N

        m_jCustomerPrice.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        m_jCustomerPriceMargin.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jMutex.setText("jCheckBox1");

        javax.swing.GroupLayout gl_jPanel2 = new javax.swing.GroupLayout(jPanel2);
        gl_jPanel2.setHorizontalGroup(
        	gl_jPanel2.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_jPanel2.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel2, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
        				.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
        				.addComponent(jLabel15, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
        				.addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jLabel5, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jLabel16, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jCustomerPriceLabel, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jLabel7, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jLabel13, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING, false)
        				.addComponent(jServiceType, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jServiceCalendar, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
        				.addGroup(gl_jPanel2.createSequentialGroup()
        					.addComponent(m_jPriceSell, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
        					.addGap(10)
        					.addComponent(m_jmargin, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE))
        				.addComponent(m_jPriceSellTax, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
        				.addGroup(gl_jPanel2.createSequentialGroup()
        					.addComponent(m_jCustomerPrice, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
        					.addGap(10)
        					.addComponent(m_jCustomerPriceMargin, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE))
        				.addComponent(m_jTax, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
        				.addComponent(m_jAtt, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
        				.addGroup(gl_jPanel2.createSequentialGroup()
        					.addGroup(gl_jPanel2.createParallelGroup(Alignment.TRAILING, false)
        						.addComponent(jRantCycleMultiple)
        						.addComponent(m_jPriceBuy, GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addComponent(jrantCycleStyle, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jEditProduct, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jMutex))
        			.addGap(207))
        );
        gl_jPanel2.setVerticalGroup(
        	gl_jPanel2.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_jPanel2.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel2)
        				.addGroup(gl_jPanel2.createSequentialGroup()
        					.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        						.addComponent(jServiceType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(jEditProduct, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(gl_jPanel2.createParallelGroup(Alignment.CENTER)
        						.addComponent(jServiceCalendar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(jLabel3)
        						.addComponent(jMutex))
        					.addGap(11)
        					.addGroup(gl_jPanel2.createParallelGroup(Alignment.BASELINE)
        						.addComponent(jLabel15)
        						.addComponent(jrantCycleStyle, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(jRantCycleMultiple, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel4)
        				.addComponent(m_jPriceBuy, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(9)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel5)
        				.addComponent(m_jPriceSell, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(m_jmargin, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(9)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel16)
        				.addComponent(m_jPriceSellTax, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(9)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jCustomerPriceLabel)
        				.addComponent(m_jCustomerPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(m_jCustomerPriceMargin, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel7)
        				.addComponent(m_jTax, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_jPanel2.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel13)
        				.addComponent(m_jAtt, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        gl_jPanel2.linkSize(SwingConstants.HORIZONTAL, new Component[] {jLabel2, jLabel3, jLabel15, jLabel7, jLabel13, jLabel4, jLabel5, jLabel16, jCustomerPriceLabel});
        jPanel2.setLayout(gl_jPanel2);

        jTabbedPane1.addTab("tab1", jPanel2);

        add(jTabbedPane1);
    }// </editor-fold>//GEN-END:initComponents

    private void jEditProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jEditProductActionPerformed
        ComboBoxValEntry selectedEntry = (ComboBoxValEntry) jServiceType.getSelectedItem();
        String refID = null;
        String showText=null;
        if (Service.SERVICETYPE_user.equalsIgnoreCase((String) selectedEntry.getM_sKey())) {//用户类
            try {
                List allUser = m_dlSales.listPeopleVisible();
                JObjectListFinder userList = JObjectListFinder.newJDialog(this);
                UserInfo tempUser = (UserInfo) userList.showUserList(allUser);
                if(tempUser!=null){
                    refID=tempUser.getId();
                    showText= AppLocal.getIntString("list.SERVICETYPE_user")+"-"+tempUser.getName();
                }
            } catch (BasicException ex) {
                MessageInf msg = new MessageInf(ex);
                msg.show(this);
            }
        } else if (Service.SERVICETYPE_place.equalsIgnoreCase((String) selectedEntry.getM_sKey())) { //资源类
            Place p=JPlaceFinder.showMessage(this, m_dlSales);
            if(p!=null){
                refID=p.getName();
                showText=AppLocal.getIntString("list.SERVICETYPE_place")+"-"+p.getName();
            }
        } else if (Service.SERVICETYPE_product.equalsIgnoreCase((String) selectedEntry.getM_sKey())) {//产品类
            Product p=JProductFinder.showMessage(this, m_dlSales);
            if(p!=null){
                refID=p.getName();
                showText=AppLocal.getIntString("list.SERVICETYPE_product")+"-"+p.getName();
            }
        }
        obj.setM_sRefId(refID);
        jServiceName.setText(showText);
}//GEN-LAST:event_jEditProductActionPerformed


    @Override
    public void refresh() {

    }

    @Override
    public Component getComponent() {
        return this;
    }

    @Override
    public void activate() throws BasicException {
        taxeslogic = new TaxesLogic(m_dlSales.list(Tax.class));

        taxcatmodel = new ComboBoxValModel(m_dlSales.list(TaxCategory.class));
        m_jTax.setModel(taxcatmodel);

        attmodel = new ComboBoxValModel(m_dlSales.list(AttributeSet.class));
        attmodel.add(0, null);
        m_jAtt.setModel(attmodel);

        serviceCalendarModel=new ComboBoxValModel(m_dlSales.list(ServiceCalendar.class));
        serviceCalendarModel.add(0,ComboBoxValEntry.buildEntry_null());
        jServiceCalendar.setModel(serviceCalendarModel);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jCustomerPriceLabel;
    private javax.swing.JButton jEditProduct;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JCheckBox jMutex;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jRantCycleMultiple;
    private javax.swing.JComboBox jServiceCalendar;
    private javax.swing.JTextField jServiceId;
    private javax.swing.JTextField jServiceName;
    private javax.swing.JComboBox jServiceType;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JComboBox jrantCycleStyle;
    private javax.swing.JComboBox m_jAtt;
    private javax.swing.JTextField m_jCustomerPrice;
    private javax.swing.JTextField m_jCustomerPriceMargin;
    private javax.swing.JTextField m_jPriceBuy;
    private javax.swing.JTextField m_jPriceSell;
    private javax.swing.JTextField m_jPriceSellTax;
    private javax.swing.JComboBox m_jTax;
    private javax.swing.JTextField m_jmargin;
    // End of variables declaration//GEN-END:variables


    @Override
    public Service createNew(Properties prop) {
        Service obj=new Service();
        obj.setM_sRefId(sdf.format(new Date()));
        return obj;
    }

    @Override
    public void rebuild(Service obj) throws BasicException {
         obj.setReference(jServiceId.getText());
         obj.setM_sName(jServiceName.getText());
         obj.setM_sType((String) serviceTypeModel.getSelectedKey());
         obj.setRantCycleStyle((String) rantCycleStyleModel.getSelectedKey());
         obj.setM_dCost((Double) Formats.CURRENCY.parseValue(m_jPriceBuy.getText(), 0));
         obj.setCyclePrice((Double) Formats.CURRENCY.parseValue(m_jPriceSell.getText(), 0));
         obj.setCustomerCyclePrice((Double) Formats.CURRENCY.parseValue(m_jCustomerPrice.getText(), 0));
         obj.setRoundRule((Integer)roundRuleModel.getSelectedKey());
         obj.setServiceCalenderId((String)serviceCalendarModel.getSelectedKey());
         obj.setTaxcategoryid((String)taxcatmodel.getSelectedKey());
         obj.setAttributeSetId((String)attmodel.getSelectedKey());
         obj.setMutex(jMutex.isSelected());
         obj.setRantCycleMultiple((Integer)Formats.INT.parseValue(jRantCycleMultiple.getText()));
    }

    @Override
    public void updateUI(boolean editAble, int state) {
        reportlock = true;
        jServiceId.setText(obj.getReference());
        jServiceName.setText(SerivceHelper.printServiceName(obj));
        serviceTypeModel.setSelectedKey(obj.getM_sType());
        jRantCycleMultiple.setText(null);
        m_jPriceSell.setText(Formats.CURRENCY.formatValue(obj.getCyclePrice()));
        m_jCustomerPrice.setText(Formats.CURRENCY.formatValue(obj.getCustomerCyclePrice()));
        rantCycleStyleModel.setSelectedKey(obj.getRantCycleStyle());
        serviceCalendarModel.setSelectedKey(obj.getServiceCalenderId());
        roundRuleModel.setSelectedKey(obj.getRoundRule());
        serviceCalendarModel.setSelectedKey(obj.getServiceCalenderId());
        taxcatmodel.setSelectedKey(obj.getTaxcategoryid());
        attmodel.setSelectedItem(obj.getAttributeSetId());
        jMutex.setSelected(obj.isMutex());
        jRantCycleMultiple.setText(Formats.INT.formatValue(obj.getRantCycleMultiple()));
        reportlock = false;
        jServiceId.setEnabled(editAble);
        jServiceType.setEnabled(editAble);
        jEditProduct.setEnabled(editAble);
        jrantCycleStyle.setEnabled(editAble);
        jRantCycleMultiple.setEnabled(editAble);
        m_jPriceSell.setEnabled(editAble);
        m_jCustomerPrice.setEnabled(editAble);
    }


      private void calculateCustomerPriceMargin(){
        if (!reportlock) {
            reportlock = true;
            Double dPriceBuy = readCurrency(m_jPriceBuy.getText());
            Double dPriceSell = (Double)readCurrency(m_jCustomerPrice.getText());

            if (dPriceBuy == null || dPriceSell == null) {
                m_jCustomerPriceMargin.setText(null);
            } else {
                m_jCustomerPriceMargin.setText(Formats.PERCENT.formatValue(new Double(dPriceSell.doubleValue() / dPriceBuy.doubleValue() - 1.0)));
            }
            reportlock = false;
        }
    }

     private void calculateCustomerPriceSellfromMargin() {
         if (!reportlock) {
            reportlock = true;
            Double dPriceBuy = readCurrency(m_jPriceBuy.getText());
            Double dMargin = readPercent(m_jCustomerPriceMargin.getText());

            if (dMargin == null || dPriceBuy == null) {
                m_jCustomerPrice.setText(null);
            } else {
                m_jCustomerPrice.setText(Formats.CURRENCY.formatValue(new Double(dPriceBuy.doubleValue() * (1.0 + dMargin.doubleValue()))));
            }
           reportlock = false;
        }
    }

    private void calculateMargin() {

        if (!reportlock) {
            reportlock = true;

            Double dPriceBuy = readCurrency(m_jPriceBuy.getText());
            Double dPriceSell = (Double) pricesell;

            if (dPriceBuy == null || dPriceSell == null) {
                m_jmargin.setText(null);
            } else {
                m_jmargin.setText(Formats.PERCENT.formatValue(new Double(dPriceSell.doubleValue() / dPriceBuy.doubleValue() - 1.0)));
            }
            reportlock = false;
        }
    }

    private void calculatePriceSellTax() {

        if (!reportlock) {
            reportlock = true;

            Double dPriceSell = (Double) pricesell;

            if (dPriceSell == null) {
                m_jPriceSellTax.setText(null);
            } else {
                double dTaxRate = taxeslogic.getTaxRate((TaxCategory) taxcatmodel.getSelectedItem());
                m_jPriceSellTax.setText(Formats.CURRENCY.formatValue(new Double(dPriceSell.doubleValue() * (1.0 + dTaxRate))));
            }
            reportlock = false;
        }
    }

    private void calculatePriceSellfromMargin() {

        if (!reportlock) {
            reportlock = true;

            Double dPriceBuy = readCurrency(m_jPriceBuy.getText());
            Double dMargin = readPercent(m_jmargin.getText());

            if (dMargin == null || dPriceBuy == null) {
                setPriceSell(null);
            } else {
                setPriceSell(new Double(dPriceBuy.doubleValue() * (1.0 + dMargin.doubleValue())));
            }

            reportlock = false;
        }

    }

    private void calculatePriceSellfromPST() {

        if (!reportlock) {
            reportlock = true;

            Double dPriceSellTax = readCurrency(m_jPriceSellTax.getText());

            if (dPriceSellTax == null) {
                setPriceSell(null);
            } else {
                double dTaxRate = taxeslogic.getTaxRate((TaxCategory) taxcatmodel.getSelectedItem());
                setPriceSell(new Double(dPriceSellTax.doubleValue() / (1.0 + dTaxRate)));
            }

            reportlock = false;
        }
    }

    private void setPriceSell(Object value) {
        if (!priceselllock) {
            priceselllock = true;
            pricesell = value;
            m_jPriceSell.setText(Formats.CURRENCY.formatValue(pricesell));
            priceselllock = false;
        }
    }

    @Override
    public Class getEditClass() {
        return Service.class;
    }

       private class PriceSellManager implements DocumentListener {
        public void changedUpdate(DocumentEvent e) {
            if (!priceselllock) {
                priceselllock = true;
                pricesell = readCurrency(m_jPriceSell.getText());
                priceselllock = false;
            }
            calculateMargin();
            calculatePriceSellTax();
        }
        public void insertUpdate(DocumentEvent e) {
            if (!priceselllock) {
                priceselllock = true;
                pricesell = readCurrency(m_jPriceSell.getText());
                priceselllock = false;
            }
            calculateMargin();
            calculatePriceSellTax();
        }
        public void removeUpdate(DocumentEvent e) {
            if (!priceselllock) {
                priceselllock = true;
                pricesell = readCurrency(m_jPriceSell.getText());
                priceselllock = false;
            }
            calculateMargin();
            calculatePriceSellTax();
        }
    }


    private class FieldsManager implements DocumentListener, ActionListener {
        public void changedUpdate(DocumentEvent e) {
            calculateMargin();
            calculatePriceSellTax();
            calculateCustomerPriceMargin();
        }
        public void insertUpdate(DocumentEvent e) {
            calculateMargin();
            calculatePriceSellTax();
            calculateCustomerPriceMargin();
        }
        public void removeUpdate(DocumentEvent e) {
            calculateMargin();
            calculatePriceSellTax();
            calculateCustomerPriceMargin();
        }
        public void actionPerformed(ActionEvent e) {
            calculateMargin();
            calculatePriceSellTax();
            calculateCustomerPriceMargin();
        }
    }

    private class PriceTaxManager implements DocumentListener {
        public void changedUpdate(DocumentEvent e) {
            calculatePriceSellfromPST();
            calculateMargin();
        }
        public void insertUpdate(DocumentEvent e) {
            calculatePriceSellfromPST();
            calculateMargin();
        }
        public void removeUpdate(DocumentEvent e) {
            calculatePriceSellfromPST();
            calculateMargin();
        }
    }

    private class MarginManager implements DocumentListener  {
        public void changedUpdate(DocumentEvent e) {
            calculatePriceSellfromMargin();
            calculatePriceSellTax();
        }
        public void insertUpdate(DocumentEvent e) {
            calculatePriceSellfromMargin();
            calculatePriceSellTax();
        }
        public void removeUpdate(DocumentEvent e) {
            calculatePriceSellfromMargin();
            calculatePriceSellTax();
        }
    }

     private class CustomerMarginManager implements DocumentListener  {
        public void changedUpdate(DocumentEvent e) {
           calculateCustomerPriceSellfromMargin();
        }
        public void insertUpdate(DocumentEvent e) {
           calculateCustomerPriceSellfromMargin();
        }
        public void removeUpdate(DocumentEvent e) {
            calculateCustomerPriceSellfromMargin();
        }
    }

     private class CustomerPriceSellManager implements DocumentListener {
        public void changedUpdate(DocumentEvent e) {
            calculateCustomerPriceMargin();
        }
        public void insertUpdate(DocumentEvent e) {
            calculateCustomerPriceMargin();
        }
        public void removeUpdate(DocumentEvent e) {
            calculateCustomerPriceMargin();
        }
    }

    private final static Double readCurrency(String sValue) {
        try {
            return (Double) Formats.CURRENCY.parseValue(sValue);
        } catch (BasicException e) {
            return 0.0;
        }
    }

    private final static Double readPercent(String sValue) {
        try {
            return (Double) Formats.PERCENT.parseValue(sValue);
        } catch (BasicException e) {
            return null;
        }
    }
}
